package com.cloudwise.controller;

import com.cloudwise.entity.LayUiTree;
import com.cloudwise.entity.Menu;
import com.cloudwise.entity.ReturnBean;
import com.cloudwise.service.MenuService;
import com.cloudwise.service.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 菜单权限表(Menu)表控制层
 *
 * @author makejava
 * @since 2020-12-17 10:15:49
 */
@Controller
@RequestMapping("menu")
public class MenuController   extends BaseController{
    /**
     * 服务对象
     */
    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleMenuService roleMenuService;


    @RequestMapping("/findAllMenus")
    @ResponseBody
    /**
     * create by: Teacher陈
     * description: 查询所有的权限菜单
     * create time: 2020/7/17 16:54
     *
     * @Param:
     * @return java.util.List<com.aaa.entity.LayUiTree>
     */
    public List<LayUiTree> findAllMenus(Menu menu){
        List<LayUiTree> layUiTreeList = menuService.queryAllTreeByloginName(null);
        return layUiTreeList;
    }

    /**
     * create by: Teacher陈
     * description: 跳转到显示菜单管理页面
     * create time: 2020/7/24 15:50
     *
     * @return a
     * @Param: null
     */
    @RequestMapping("/toShowMenu")
    public String toShowMenu(){
        return "menu/showMenu";
    }

    @RequestMapping("/findAllMenusTreeTable")
    @ResponseBody
    public ReturnBean findAllMenusTreeTable(){
        List<Menu> menuList = menuService.queryAll(null);
        return returnSuccess(menuList);
    }
    /**
     * @create by: Teacher陈
     * @description: 保存菜单
     * @create time: 2020/7/21 1:16
     * @param menu
     * @return Result
     */
    @RequestMapping("/saveMenu")
    @ResponseBody
    public ReturnBean saveMenu(Menu menu){
        setCreate(menu);
        try {
            menuService.insert(menu);
            return returnSuccess(null);
        }catch (Exception e){
            return returnFail(null);
        }
    }
    /**
     * @create by: Teacher陈
     * @description: 根据menuid删除menu
     * @create time: 2020/7/21 16:08
     * @param menuId
     * @return Result
     */
    @RequestMapping("/deleteMenu")
    @ResponseBody
    public  ReturnBean deleteMenu(Integer menuId){
        //删除menu表
        boolean deleteMenu = menuService.deleteById(menuId);
        //删除role_menu中间表
      /*  Wrapper<RoleMenu> wrapper = new EntityWrapper<>();
        wrapper.eq("menu_id",menuId);*/
        boolean deleteRoleMenu = roleMenuService.deleteByMenuId(menuId);
        if (deleteMenu && deleteRoleMenu) {
            return returnSuccess(null);
        } else {
            return returnFail(null);
        }
    }
    /**
     * @create by: Teacher陈
     * @description: 按照菜单id查询菜单名称
     * @create time: 2020/7/21 20:48
     * @param menuId
     * @return Result
     */
    @RequestMapping("/findParentNameId")
    @ResponseBody
    public  ReturnBean  findParentNameId(Integer menuId){
        Menu menu = menuService.queryById(menuId);
        if(null!=menu){
            return  returnSuccess(menu);
        }else {
            //menuId为0的时候，返回的对象是空，防止返回前台为空
            Menu menuName = new Menu();
            return  returnSuccess(menuName);
        }
    }

    @RequestMapping("/updateMenu")
    @ResponseBody
    public  ReturnBean updateMenu(Menu menu){
        setUpdate(menu);
        try {
            menuService.update(menu);
            return returnSuccess(null);
        }catch (Exception e){
            return returnFail(null);
        }


    }
}